//
//  CycleAlertPresentationController.m
//  ProjectAlertView
//
//  Created by zl on 2017/11/9.
//  Copyright © 2017年 zl. All rights reserved.
//

#import "CycleAlertPresentationController.h"

@interface CycleAlertPresentationController()

@end

@implementation CycleAlertPresentationController


/**  懒加载“背景视图”  */
- (UIView *)coverView {
    
    if (!_coverView) {

        _coverView = [UIView new];
        _coverView.backgroundColor = [UIColor blackColor];
        _coverView.alpha = 0.4;
        _coverView.userInteractionEnabled = YES;
        _coverView.frame = self.containerView.bounds;
        //添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
        [_coverView addGestureRecognizer:tap];

    }
    ///是否隐藏背景
    _coverView.hidden = self.hideCoverView;
    ///背景色，透明度
    _coverView.backgroundColor = self.coverViewBgColor;
    _coverView.alpha = self.coverViewAlpha;
    
    return _coverView;
}
///控制背景视图的显示和隐藏
//-(void)setCoverView:(UIView *)coverView{
//    coverView.hidden = self.hideCoverView;
//}
//-(void)setHideCoverView:(BOOL)hideCoverView{
//    _hideCoverView = hideCoverView;
//    self.coverView.hidden = hideCoverView;
////    NSLog(@"%zd",hideCoverView);
//}

-(void)containerViewWillLayoutSubviews{
    [super containerViewWillLayoutSubviews];

//    NSLog(@"%f  %f  %f  %f",X,Y,presented_W,presented_H);
    ///设置弹出视图的尺寸
    self.presentedView.frame = self.presentedFrame;
    
//    self.presentedView.frame = CGRectMake(100   , 100, 200, 200);
    ///背景视图
    [self.containerView addSubview:self.coverView];
    [self.containerView insertSubview:self.coverView atIndex:0];
    
    ///是否可以点击背景View
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(noteClick:) name:@"kCanClickBgViewNotification" object:nil];
    ///监听改变圆角的通知
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeCornerRadiusClick:) name:@"kChangePresentedViewCornerRadiusNotification" object:nil];
    

}

/// 点击背景View
- (void)noteClick:(NSNotification *)note{
    BOOL flg = note.object;
    self.canClickBgView = flg;
    [self dismiss];
}

///设置圆角
- (void)setCornerRadius:(double)cornerRadius {
    _cornerRadius = cornerRadius;
    self.presentedView.layer.cornerRadius = self.cornerRadius;
    self.presentedView.layer.masksToBounds = YES;
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

///退出弹出视图
- (void)dismiss{
    //当被设置成不可点击的时候
    if (self.canClickBgView) {
        [self.coverView removeFromSuperview];
        [self.presentedViewController dismissViewControllerAnimated:YES completion:^{
            NSLog(@"you tap me,and i have successed hide the coverView");
        }];
    }
}

///给弹出视图某个方向的角切圆角
- (void)cycle_cornerByRoundingCorners:(UIRectCorner)corners cornerRadius:(CGFloat)cornerRadius {
    CGRect rect = CGRectMake(self.presentedView.bounds.origin.x, self.presentedView.bounds.origin.y, self.presentedFrame.size.width, self.presentedView.bounds.size.height);
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
    CAShapeLayer *maskLayer = [CAShapeLayer new];
    maskLayer.frame = rect;
    maskLayer.path = maskPath.CGPath;
    self.presentedView.layer.mask = maskLayer;
    self.presentedView.layer.contentsScale = [[UIScreen mainScreen] scale];
}













@end
